package de.java.shortys.myhome.fuelmonitor.controller;

import static de.java.shortys.myhome.fuelmonitor.lang.LanguageConstants.FUELFILLING_OVERVIEW;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import de.java.shortys.myhome.api.controller.AbstractSwingModuleController;
import de.java.shortys.myhome.api.controller.AbstractSwingSubModuleController;
import de.java.shortys.myhome.api.controller.IController;
import de.java.shortys.myhome.fuelmonitor.model.IFuelFilling;
import de.java.shortys.myhome.fuelmonitor.view.SubModuleViewOverview;
import de.java.shortys.myhome.language.controller.LanguageManager;

public class SubModuleControllerOverview extends AbstractSwingSubModuleController {

	public static final String ID = "FuelMonitor_Overview";

	private SubModuleControllerFillingEdit subFillingEdit;

	private IFuelFilling selection;

	public SubModuleControllerOverview(AbstractSwingModuleController root, IController parent) {
		super(root, parent, new SubModuleViewOverview());
		initialize();
	}

	@Override
	public IController getParent() {
		return null;
	}

	@Override
	public boolean allowActivate() {
		return true;
	}

	@Override
	public void collapse() {
		// TODO Auto-generated method stub

	}

	@Override
	public void expand() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getIcon() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTitle() {
		return LanguageManager.getInstance(FuelMonitorController.NAME).get(FUELFILLING_OVERVIEW);
	}

	@Override
	public void initialize() {
		subFillingEdit = new SubModuleControllerFillingEdit(getRoot(), this);
		onInitialize();
	}

	@Override
	public void onActivate() {
		// TODO Auto-generated method stub
	}

	@Override
	public void onDeactivate() {
		// TODO Auto-generated method stub
	}

	@Override
	public void onInitialize() {
		FuelMonitorController rootController = (FuelMonitorController) getRoot();
		SubModuleViewOverview view = (SubModuleViewOverview) getView();
		view.put(rootController.getFuelMonitorService().read());
		initializeTableListeners(view);
		initializeListSelectionListener(view);
		initializeEditListener(view);
		initializeDeleteListener(view);
	}

	private void initializeTableListeners(SubModuleViewOverview view) {
		view.addDoubleClickListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					editAction();
				}
			}
		});
	}

	private void initializeDeleteListener(SubModuleViewOverview view) {
		view.addDeleteListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				FuelMonitorController rootController = (FuelMonitorController) getRoot();
				rootController.deleteFuelFilling(selection);
			}
		});
	}

	private void initializeEditListener(SubModuleViewOverview view) {
		view.addEditListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				editAction();
			}

		});
	}

	private void editAction() {
		subFillingEdit.setFuelFilling(selection);
		subFillingEdit.activate();
	}

	private void initializeListSelectionListener(final SubModuleViewOverview view) {
		view.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int selectedRow = view.getTable().getSelectedRow();
				if (selectedRow != -1) {
					FuelMonitorController rootController = (FuelMonitorController) getRoot();
					final String key = (String) view.getTable().getValueAt(selectedRow, 0);
					selection = rootController.getFuelFillingByKey(key);
					view.getToolbarItemEdit().setEnabled(true);
					view.getToolbarItemDelete().setEnabled(true);
				} else {
					selection = null;
					view.getToolbarItemEdit().setEnabled(false);
					view.getToolbarItemDelete().setEnabled(false);
				}
			}
		});
	}

	@Override
	public boolean list() {
		return true;
	}

	@Override
	public String getId() {
		return ID;
	}

}
